package com.xdy.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xdy.reggie.common.CustomException;
import com.xdy.reggie.dto.SetMealDto;
import com.xdy.reggie.entity.SetMeal;
import com.xdy.reggie.entity.SetMealDish;
import com.xdy.reggie.mapper.SetMealMapper;
import com.xdy.reggie.service.SetMealDishService;
import com.xdy.reggie.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, SetMeal> implements SetMealService {

    @Resource
    private SetMealDishService setMealDishService;

    /**
     * 添加套餐信息
     * @param setMealDto
     */
    @Transactional
    @Override
    public void saveWithSetMealDish(SetMealDto setMealDto) {
        //先查询是否存在已删除的同名菜品
        LambdaQueryWrapper<SetMeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetMeal::getName,setMealDto.getName());
        SetMeal one = getOne(queryWrapper);
        List<SetMealDish> setMealDishes = setMealDto.getSetmealDishes();
        if (one != null && one.getIsDeleted() == 1){
            BeanUtils.copyProperties(setMealDto,one,"id");
            updateIsDeleted(one);
            LambdaQueryWrapper<SetMealDish> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.eq(SetMealDish::getSetmealId,one.getId());
            setMealDishService.remove(dishFlavorLambdaQueryWrapper);
            setMealDishes = setMealDishes.stream().map(item -> {
                item.setSetmealId(one.getId());
                return item;
            }).collect(Collectors.toList());
        }else{
            baseMapper.insert(setMealDto);
            setMealDishes = setMealDishes.stream().map(item -> {
                item.setSetmealId(setMealDto.getId());
                return item;
            }).collect(Collectors.toList());
        }
        setMealDishService.saveBatch(setMealDishes);
    }

    /**
     * 该套餐已删除，重新添加
     * @param one
     */
    private void updateIsDeleted(SetMeal one) {
        one.setIsDeleted(0);
        updateById(one);
    }

    @Override
    @Transactional
    public void updateWithSetMealDish(SetMealDto setMealDto) {
        //更新套餐信息
        updateById(setMealDto);

        //删除套餐菜品关联信息
        LambdaQueryWrapper<SetMealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetMealDish::getSetmealId,setMealDto.getId());
        setMealDishService.remove(queryWrapper);
        //添加新的套餐菜品关联信息
        List<SetMealDish> setmealDishes = setMealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(item -> {
            item.setSetmealId(setMealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setMealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐信息以及关联菜品信息
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithSetMealDish(List<Long> ids) {
        log.info("删除的id ===> {}",ids);
        log.info("菜品ID ===> {}",ids);
        List<SetMeal> setMealList = listByIds(ids);
        boolean allMatch = setMealList.stream().allMatch(item -> item.getStatus() == 0);//判断要删除的菜品是否都是停售状态
        if (!allMatch){
            throw new CustomException("套餐正在售卖，请先停售！");
        }
        List<SetMealDish> setMealDishes = new ArrayList<>();
        List<SetMeal> setMealList1 = ids.stream().map(item -> {
            SetMeal setMeal = new SetMeal();
            SetMealDish setMealDish = new SetMealDish();
            setMeal.setId(item);
            setMeal.setIsDeleted(1);
            setMealDish.setSetmealId(item);
            setMealDish.setIsDeleted(1);
            setMealDishes.add(setMealDish);
            return setMeal;
        }).collect(Collectors.toList());
        updateBatchById(setMealList1);
        for (SetMealDish setMealDish : setMealDishes) {
            LambdaQueryWrapper<SetMealDish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SetMealDish::getSetmealId,setMealDish.getSetmealId());
            setMealDishService.update(setMealDish,queryWrapper);
        }
    }


}
